#include <bits/stdc++.h>

using namespace std;

int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0};

void dfs(vector<vector<int>> &graph, vector<vector<bool>> &visited, int row, int col, bool flag)
{
    if (graph[row][col] == 0 || visited[row][col] == true)
    {
        return;
    }
    visited[row][col] = true;
    if (flag)
    {
        graph[row][col] = 0;
    }
    for (int i = 0; i < 4; i++)
    {
        int next_row = row + dir[i][0];
        int next_col = col + dir[i][1];

        if (next_row < 0 || next_row >= (int)graph.size() || next_col < 0 || next_col >= (int)graph[0].size())
        {
            continue;
        }
        dfs(graph, visited, next_row, next_col, flag);
    }
}

int main()
{
    int n, m;
    cin >> n >> m;
    vector<vector<int>> graph(n, vector<int>(m, 0));
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cin >> graph[i][j];
        }
    }
    vector<vector<bool>> visited(n, vector<bool>(m, false));
    for (int i = 0; i < n; i++)
    {
        if (graph[i][0] == 1 && visited[i][0] == false)
        {
            dfs(graph, visited, i, 0, false);
        }
        if (graph[i][m - 1] == 1 && visited[i][m - 1] == false)
        {
            dfs(graph, visited, i, m - 1, false);
        }
    }
    for (int j = 0; j < m; j++)
    {
        if (graph[0][j] == 1 && visited[0][j] == false)
        {
            dfs(graph, visited, 0, j, false);
        }
        if (graph[n - 1][j] == 1 && visited[n - 1][j] == false)
        {
            dfs(graph, visited, n - 1, j, false);
        }
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (graph[i][j] == 1 && visited[i][j] == false)
            {
                dfs(graph, visited, i, j, true);
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cout << graph[i][j] << " ";
        }
        cout << endl;
    }
    return 0;
}